package astar;

import domain.Agent;
import domain.Flag;
import domain.Game.direction;
import domain.Square;
import domain.exceptions.NoSuchSquareException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Vector;

/**
 *
 * @author shaigi
 */
public class ANode implements Comparable {
    
    private Vector<Agent>           _agents;
    private Vector<Flag>            _flags;
    private int                     _g;         // the effort value till now
    private LinkedList<direction>   _path;
    private ANode                   _up;
    private ANode                   _right;
    private ANode                   _down;
    private ANode                   _left;
    

    public ANode(Vector<Agent> _agents, Vector<Flag> _flags, int g, LinkedList<direction> path) {
        this._agents = _agents;
        this._flags = _flags;
        this._g     = g;
        this._path  = path;
    }
    
    public ANode simulateNode(direction dir, Agent me){
        ANode ans = null;
        
        // constructing agents vector:
        Vector<Agent> agentsVector = new Vector<Agent>();
        
        
        
        
        // constructing flags vector:
        Vector<Flag> flagsVector = null;
        Square nextSquare = null;
        try {
            nextSquare = me.getSquare().neighbour(dir);
            if ( nextSquare.isContainFlag() ){
                Flag flagToRemove = nextSquare.getFlag();
                flagsVector = (Vector<Flag>)_flags.clone();
                for (Iterator<Flag> it = flagsVector.iterator(); it.hasNext();) {
                    Flag flag = it.next();
                    if ( flag.equals(flagToRemove))
                }
            }
        } catch (NoSuchSquareException ex) {
            
        }
        //Vector<Flag> flagsVector = (Vector<Flag>)_flags.clone();
        
        
        
        
        // constructing new g:
        int newG = 0;
        
        
        
        
        // constructing new actions path:
        LinkedList<direction> newPath = new LinkedList<direction>();
        
        
        
        
        
        // constructing the actual new node:
        ans = new ANode(agentsVector, flagsVector, newG, newPath);
        
        return ans;
    }
    
    private int computeHeuristic(){
        int ans = -1;
        //TODO
        return ans;
    }
    
    private int computeF(){
        int ans = -1;
        //TODO: change here:
        ans = this._g + computeHeuristic();
        
        
        return ans;
    }
    
    public void expand(){
        
    }

    public int compareTo(Object o) {
        ANode other = (ANode)o;
        
        if ( this == other ){
            return 0;
        }
        
        int thisF = this.computeF();
        int otherF = other.computeF();
        
        //TODO check if the order is correct:
        if ( thisF > otherF )
            return 1;
        else if ( thisF < otherF )
            return -1;
        else
            return 0;
    }

    public LinkedList<direction> getPath() {
        return _path;
    }

    public Vector<Agent> getAgents() {
        return _agents;
    }

    public Vector<Flag> getFlags() {
        return _flags;
    }

    public int getG() {
        return _g;
    }

}
